﻿    function LoadProjects(openProjectValue, closeProjectValue) {
        $.ajax({
            url: "/EnTimeLog/GetProjectPartial",
            type: 'POST',
            data: { chkOpenProjects: openProjectValue, chkClosedProjects: closeProjectValue },
            success: function (response) {
                $("#ajaxDropDownListProject").replaceWith(response);
                ClearGridData();
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    function ClockOn(mCategory, mActivity, mReason) {
        $.ajax({
            url: "/Common/ClockOn",
            type: 'POST',
            data: { category: mCategory, activity: mActivity, reason: mReason },
            success: function (response) {

                // enable resume previous task
                $("#btnResumePreviousTask").removeAttr("disabled");

                alert("Successfully clocked on!");

            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    function ClockOff() {
        $.ajax({
            url: "/Common/ClockOff",
            type: 'POST',
            data: {},
            success: function (response) {
                alert("Successfully clocked off!");
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Resume previous task   */
    function ResumePreviousTask() {
        $.ajax({
            url: "/EnTimeLog/GetLastProject",
            type: 'POST',
            data: {},
            success: function (response) {
                $("#ajaxDropDownListProject").replaceWith(response);

            },
            error: function (xhr) {
                alert("Error!");
            }
        });
        $.ajax({
            url: "/EnTimeLog/GetLastCategory",
            type: 'POST',
            data: {},
            success: function (response) {
                $("#ajaxGridCategory").replaceWith(response);
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
        $.ajax({
            url: "/EnTimeLog/GetLastTask",
            type: 'POST',
            data: {},
            success: function (response) {
                $("#ajaxGridTask").replaceWith(response);
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
        $.ajax({
            url: "/EnTimeLog/GetLastProjectDetail",
            type: 'POST',
            data: {},
            success: function (response) {

                // Split response to array of values
                var array = response.split("#");

                // Replace old values with new ones
                $("#inputEngineer").val(array[0]);
                $("#inputLeadEngineer").val(array[1]);
                $("#inputToStart").val(array[2]);
                $("#inputToFinish").val(array[3]);
                $("#inputStatus").val(array[4]);
                $("#inputCompleted").val(array[5]);

            },
            error: function (xhr) {
                alert("Error!");
            }
        });
        $.ajax({
            url: "/EnTimeLog/GetLastOtherEngineers",
            type: 'POST',
            data: {},
            success: function (response) {
                $("#ajaxListOtherEngineers").replaceWith(response);
            },
            error: function (xhr) {
                alert("Error!");
            }
        });

        // Ajax function
        $.ajax({
            url: "/EnTimeLog/LogOnToLastTask",
            type: 'POST',
            data: {
            },
            success: function (response) {
                alert('Successfully resumed previous task');
            },
            error: function (xhr) {
                alert("Error!");
            }
        });

    }

    /*  Get project list by user    */
    function GetProjectListByUserId(userId) {
        $.ajax({
            url: "/EnTimeLog/GetProjectPartialByUser",
            type: 'POST',
            data: { userId: userId },
            success: function (response) {
                $("#ajaxDropDownListProject").replaceWith(response);
                ClearGridData();
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Get project detailed information    */
    function GetProjectDetail(pr_ref) {
        $.ajax({
            url: "/EnTimeLog/GetProjectDetail",
            type: 'POST',
            data: { projectId: pr_ref },
            success: function (response) {

                // Split response to array of values
                var array = response.split("#");

                // Replace old values with new ones
                $("#inputEngineer").val(array[0]);
                $("#inputLeadEngineer").val(array[1]);
                $("#inputToStart").val(array[2]);
                $("#inputToFinish").val(array[3]);
                $("#inputStatus").val(array[4]);
                $("#inputCompleted").val(array[5]);


            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Get project categories    */
    function GetProjectCategories(pr_ref) {
        $.ajax({
            url: "/EnTimeLog/GetCategory",
            type: 'POST',
            data: { projectId: pr_ref },
            success: function (response) {
                $("#ajaxGridCategory").replaceWith(response);
                $("#ajaxGridTask").replaceWith("<div id='ajaxGridTask'></div>");
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    function GetReason(promptMessage, blankMessageNotify, maxLength) {
        var x;
        var result;
        var reason = "";

        do {
            result = prompt(promptMessage, reason);

            // trim the prompt message
            result = result.replace(/^\s+|\s+$/g, '');
            if (result!=null)
            {
                // Remove single quotes
//                var i = result.indexOf("'");
//                if (i > 0) {
//                    alert('Single quotes are not allowed - these have been automatically removed.');
//                    result = result.replace("'", "");
//                }

                if(result.length > maxLength) {
                    var alertString = "You have entered " + result.length + " characters.  The maximum is " + maxLength + ".";
                    alert(alertString);
                 }
            }
            if (result == "") {
                reason = blankMessageNotify;
            }
        }
        // The loop condition will be broken if result < maxLength , result != null ( Cancel value ) and result != blank string
        while (result != null && (result.length > maxLength || result == ""));


        return result;
    }
    /*  Get other engineers in the selected projects    */
    function GetListOtherEngineers(pr_ref) {
        $.ajax({
            url: "/EnTimeLog/GetOtherEngineers",
            type: 'POST',
            data: { projectId: pr_ref },
            success: function (response) {
                $("#ajaxListOtherEngineers").replaceWith(response);
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    function CheckLogOnStatus(isAlert) {
        $.ajax({
            url: "/EnTimeLog/CheckLogOnStatus",
            type: 'POST',
            data: {},
            success: function (response) {
                if (response != null && response != "" && isAlert == true) {
                    alert(response);
                }
                else {
                    // Else : already logon to an activity
                    // -> enable button Resume Previous Task
                    $("#btnResumePreviousTask").removeAttr("disabled");
                }
            },
            error: function (xhr) {
                alert("CheckLogOnStatus Error!");
            }
        });
    }

    function GetProjectData() {
        if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {

            // Get project referenced value
            var pr_ref = $("select#ddlProject").val();

            // Get project detailed info and categories, other engineers
            GetProjectDetail(pr_ref);
            //GetProjectCategories(pr_ref);
            //GetListOtherEngineers(pr_ref);

            // Ajax function
            $.ajax({
                url: "/EnTimeLog/GetTask",
                type: 'POST',
                data: {
                    projectId: pr_ref
                },
                success: function (response) {
                    $("#ajaxGridTask").replaceWith(response)
                },
                error: function (xhr) {
                    alert("Error!");
                }
            });

        }
        else {
            // Clear all grid's data
            ClearGridData();
        }
    }


    /* Free entry for admin role */

    //    function FreeEntryAdmin() {
    //        $.ajax({
    //            url: "/FreeEntry/IndexAdm",
    //            type: 'POST',
    //            data: { 
    //                project: $("select#ddlProject").val(), 
    //                category: $("#ajaxGridCategory tbody tr .selected").find('td:second').text(),
    //                task: $("#ajaxGridTask tbody tr .selected").find('td:second').text(),
    //                selectedEngineer: $("select#ddlEngineers").val()
    //            },
    //            success: function (response) {
    //                $("#ajaxListOtherEngineers").replaceWith(response);
    //            },
    //            error: function (xhr) {
    //                alert("Error!");
    //            }
    //        });
    //    }

    /*  Clear all grid data    */
    function ClearGridData() {
        $("#ajaxGridCategory").replaceWith("<div id='ajaxGridCategory'></div>");
        $("#ajaxGridTask").replaceWith("<div id='ajaxGridTask'></div>");
        $("#ajaxListOtherEngineers").replaceWith("<div id='ajaxListOtherEngineers'></div>");
    }